
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <sstream>
#include <SFML/Graphics.hpp>
#include "include/Brique.hpp"
#include "include/Carapace.hpp"
#include "include/Palette.hpp"
#include "include/FireBall.hpp"
#include "include/Screen.hpp"
#include "include/bib/GestData.hpp"
#include "include/bib/BibSprite.hpp"
#include "include/Game.hpp"
#include "include/Menu.hpp"
#include "include/HeaderScreen.hpp"


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
      //freopen("stderr.txt","w",stderr);
//    // Create the main rendering window
//    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Mario Brakout");
//    BibSprite bibSprite;
//    Brique brique(1,200,300,bibSprite.getImageBrique());
//    Carapace carap(300,400,bibSprite.getImageCarap());
//    Palette palette(10,550,bibSprite.getImagePalette());
//    sf::Sprite Fond(bibSprite.getImageBackground());
//    sf::String textInfo("test",bibSprite.getFontMario(),25);
//    /*sf::Image imageBrique;
//    sf::Image imageCarap;
//    sf::Image imageFond;
//    sf::Image imagePalette;
//
//    imageBrique.LoadFromFile("images/briques/briques.png");
//    imageCarap.LoadFromFile("images/carapace/carap.png");
//    imageFond.LoadFromFile("images/fond/fond1.png");
//    imagePalette.LoadFromFile("images/palette/palette.png");*/
//
//
//    textInfo.SetPosition(0,0);
//    App.SetFramerateLimit(60);
//
//
//    float perc = 0;
//    // Start game loop
//    while (App.IsOpened())
//    {
//        // Process events
//        sf::Event Event;
//        while (App.GetEvent(Event))
//        {
//            // Close window : exit
//            if (Event.Type == sf::Event::Closed)
//                App.Close();
//
//            if (Event.Type == sf::Event::KeyPressed){
//                switch(Event.Key.Code){
//
//                    case(sf::Key::D):
//                        carap.upVitesse();
//                        break;
//                    case(sf::Key::F):
//                        carap.downVitesse();
//                        break;
//                    case(sf::Key::C):
//                        carap.upAngle();
//                        break;
//                    case(sf::Key::V):
//                        carap.downAngle();
//                        break;
//                    case(sf::Key::E):
//                        palette.setWidth(palette.getWidth()-1);
//                        break;
//                    case(sf::Key::R):
//                        palette.setWidth(palette.getWidth()+1);
//                        break;
//                    default:
//                        break;
//                }
//            }
//            if (Event.Type == sf::Event::MouseMoved){
//                palette.setPosition(Event.MouseMove.X);
//            }
//        }
//
//        // Move the sprite
//        if (App.GetInput().IsKeyDown(sf::Key::Escape)) App.Close();
//
//        //Sprite.SetPosition(App.GetInput().GetMouseX(),App.GetInput().GetMouseY());
//        carap.moveCarap();
//        std::ostringstream oss;
//        oss << "Vx: " << carap.getVitesseX() << " Vy: " << carap.getVitesseY()<<" angle: "<<carap.getAngle() << " perc: " << perc << " %";
//        textInfo.SetText(oss.str());
//        textInfo.SetFont(bibSprite.getFontMario());
//
//        // Display sprite in our window
//        App.Draw(Fond);
//        App.Draw(brique.getSprite());
//
//        sf::Sprite palSprite = palette.getSprite();
//        palSprite.SetPosition(palette.getPositionObj().Left,palette.getPositionObj().Top);
//        palSprite.SetSubRect(palette.getRect(0));
//        App.Draw(palSprite);
//        palSprite.SetSubRect(palette.getRect(1));
//        int i;
//        for(i=0;i<palette.getWidth();i++){
//            palSprite.SetPosition(palette.getPositionObj().Left+LARGSPRITEG+(i*LARGSPRITEC),palette.getPositionObj().Top);
//            App.Draw(palSprite);
//        }
//        palSprite.SetSubRect(palette.getRect(2));
//        palSprite.SetPosition(palette.getPositionObj().Left+LARGSPRITEG+((i-1)*LARGSPRITEC)+LARGSPRITED,palette.getPositionObj().Top);
//
//        //fprintf(stderr,"%f3.2 %f3.2 %f3.2 %f3.2\n", palette.getPositionObj().Left,palette.getPositionObj().Top,palette.getPositionObj().Right,palette.getPositionObj().Bottom);
//        //fprintf(stderr,"%f3.2 %f3.2 %f3.2 %f3.2\n", carap.getPositionObj().Left,carap.getPositionObj().Top,carap.getPositionObj().Right,carap.getPositionObj().Bottom);
//        sf::IntRect overlapRect;
//        if(palette.getPositionObj().Intersects(carap.getPositionObj(),&overlapRect)){
//            /*fprintf(stderr,"palette:     Left: %d Top: %d Right: %d Bottom: %d\n", palette.getPositionObj().Left,palette.getPositionObj().Top,palette.getPositionObj().Right,palette.getPositionObj().Bottom);
//            fprintf(stderr,"Carap:       Left: %d Top: %d Right: %d Bottom: %d\n",   carap.getPositionObj().Left,  carap.getPositionObj().Top,  carap.getPositionObj().Right,  carap.getPositionObj().Bottom);
//            fprintf(stderr,"OverLapRect: Left: %d Top: %d Right: %d Bottom: %d\n\n",            overlapRect.Left,             overlapRect.Top,             overlapRect.Right,             overlapRect.Bottom);*/
//            float centerCarap = carap.getPositionObj().Left+(LARGSPRITECARAP/2);
//            float centerPalette = palette.getPositionObj().Left+(palette.getWidthPx()/2);
//            perc = ((centerCarap-centerPalette)/(centerPalette-palette.getPositionObj().Left))*100;
//            //sprintf(aff,"Rebond: %3.2f %% du milieu de la palette",perc);
//            //ecriture->setInfo(aff);
//
//            double normale = pow((perc/100),3)*20;
//            carap.setAngle(2*normale-carap.getAngle());
//        }
//
//        App.Draw(palSprite);
//
//        App.Draw(carap.getSprite());
//        App.Draw(textInfo);
//
//        // Display window contents on screen
//        App.Display();
//    }
    std::vector<Screen*> Screens;
    int screen = 0;
    int nextScreen = screen;

    //Window creation
    sf::RenderWindow App(sf::VideoMode(LARGSCREEN, HAUTSCREEN, 32), "Mario Brakout");

    //Mouse cursor no more visible
    App.ShowMouseCursor(false);
    App.SetFramerateLimit(60);

    BibSprite bibSprite;

    //Screens preparations
    Menu menu(&bibSprite);
    Screens.push_back(&menu);
    Game s1(0,&bibSprite);
    Screens.push_back (&s1);


    //Main loop
    while (screen >= 0)
    {
        nextScreen = Screens[screen]->Run(App);
        Screens[screen]->freeRessource();
        screen = nextScreen;
    }

    return EXIT_SUCCESS;
}
